package com.lab.spring.topicDatabase;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bsceran
 * Date: 5/23/13
 * Time: 9:54 AM
 * To change this template use File | Settings | File Templates.
 */
public class JdbcVehicleDao  extends SimpleJdbcDaoSupport implements VehicleDao {


    @Override
    public void insert(final Vehicle vehicle) {
        String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
                + "VALUES (?, ?, ?, ?)";

        getSimpleJdbcTemplate().update(sql, vehicle.getVehicleNo()
                                            , vehicle.getColor()
                                            , vehicle.getWheel()
                                            , vehicle.getSeat());
    }

    @Override
    public void update(Vehicle vehicle) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(Vehicle vehicle) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Vehicle findByNo(String vehicleNo) {
        String sql = "SELECT * FROM VEHICLE WHERE VEHICLE_NO = ?";
        return getSimpleJdbcTemplate().queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(Vehicle.class), vehicleNo);
    }

    @Override
    public List<Vehicle> findByNo() {
        String sql = "SELECT * FROM VEHICLE";
        return getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Vehicle.class));
    }
}
